  package com.totoro.web;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.Authentication;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.servlet.ModelAndView;

import com.totoro.domain.Project;
import com.totoro.domain.Student;
import com.totoro.domain.Tutor;
import com.totoro.service.ProjectService;
import com.totoro.service.StaffService;
import com.totoro.service.StudentService;

@Controller
@RequestMapping("/tutor")
public class TutorSelectStudentController {
	@Autowired
	private StudentService studentService;
	@Autowired
	private StaffService staffService;
	@Autowired
	private ProjectService projectService;
	//跳转到选择学生页面
	@RequestMapping("/select")
	public ModelAndView list(Authentication authentication) {
		String username = authentication.getName();
		Tutor tutor = staffService.uniqueResult("no", username);
		ModelAndView mav = new ModelAndView();
		mav.setViewName("/tutor/select");
		mav.addObject("tutorProject", tutor.getProject());
		return mav;
	}
	//选择失败跳转页面
	@RequestMapping("/selectFailed")
	public ModelAndView selectFailed() {
		
		ModelAndView mav = new ModelAndView();
		mav.setViewName("/tutor/selectFailed");
		return mav;
	}
	//执行选择
	@RequestMapping(value = "/selectStudent", method = RequestMethod.GET)
	public String selectStudent(Integer project_id,Integer student_id) {
	 	 Project project = this.projectService.get(project_id);
		 Student student = this.studentService.get(student_id);	
		
		 if(student.getProject()!=null||project.getStudent()!=null){
			 return "redirect:/tutor/selectFailed.html";
		 }
		 else{
			 student.setProject(project);
		 studentService.update(student);
		return "redirect:/tutor/select.html";
		 }
		
	}
	//执行退选
	@RequestMapping(value = "/upselectStudent", method = RequestMethod.GET)
	public String upselectStudent(Integer student_id) {
		 Student student = studentService.get(student_id);	
 		 student.setProject(null);
    	 studentService.update(student);
		return "redirect:/tutor/select.html";	
	}
	}
	
